package com.aiju.widgit.dialog;

import android.content.Context;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.base.dialog.BaseDialog;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-12-13 11:22
 * @version: V1.0
 * @Description: 商品单位，自记账预算类型
 */
public class ListDialog extends BaseDialog {
    private ListView listView;

    public ListDialog(Context context) {
        super(context);
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.LodingDialogStyle;
    }

    @Override
    protected View getView() {
        View root = mLayoutInflater.inflate(R.layout.dialog_good_unit, null);
        listView = (ListView) root.findViewById(R.id.lv_dialog_good);
        root.findViewById(R.id.tv_dialog_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return root;
    }

    public ListDialog setData(final List<String> items) {
        listView.setAdapter(new MyBaseAdapter(items));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (OnItemOnClick != null) {
                    OnItemOnClick.click(items.get(position), position);
                }
                dismiss();
            }
        });
        return this;
    }

    private OnItemOnClick OnItemOnClick;

    public ListDialog setOnItemOnClick(OnItemOnClick click) {
        this.OnItemOnClick = click;
        return this;
    }

    private String rightString;

    public ListDialog setSelectString(String rightString) {
        this.rightString = rightString;
        return this;
    }

    public interface OnItemOnClick {
        void click(String item, int position);
    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public ListDialog setLayoutParams() {
        getDialogWindow().setWindowAnimations(R.style.share_animation);
        getDialogWindow().setGravity(Gravity.BOTTOM);
        getDialogWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
        return this;
    }

    private final class MyBaseAdapter extends BaseAdapter {

        private List<String> items;

        public MyBaseAdapter(List<String> items) {
            this.items = items;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_list_item, null);
            TextView textView = (TextView) inflate.findViewById(R.id.tv_dialog_title);
            ImageView ImageView = (ImageView) inflate.findViewById(R.id.iv_select);

            textView.setText(Html.fromHtml(items.get(position)));
            if (items.get(position).equals(rightString)) {
                ImageView.setVisibility(View.VISIBLE);
            } else {
                ImageView.setVisibility(View.GONE);
            }
            return inflate;
        }
    }
}
